package com.tywj.buscustomerapp.view.myview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tywj.buscustomerapp.R;

public class ToastJoe  {
    public static ToastJoe mToastJoe;
    private Toast toast;

    private ToastJoe() {
    }

    public static ToastJoe getmToastJoe() {
        if (mToastJoe == null) {
            mToastJoe = new ToastJoe();
        }
        return mToastJoe;
    }

    /**
     * 显示
     */
    public void ToastShowRight(Context context, ViewGroup root, String str) {
        if (context!=null) {
            TextView text = null;
            View view = LayoutInflater.from(context).inflate(R.layout.toast_right_layout, root);
            text = view.findViewById(R.id.textToast);
            text.setText(str); // 设置显示文字
            toast = new Toast(context);
            toast.setGravity(Gravity.CENTER, 0, 0); // Toast显示的位置
            toast.setDuration(Toast.LENGTH_SHORT); // Toast显示的时间
            toast.setView(view);
    //        if (toast==null){
    //            View view = LayoutInflater.from(context).inflate(R.layout.toast_right_layout, root);
    //            text = view.findViewById(R.id.textToast);
    //            text.setText(str); // 设置显示文字
    //            toast = new Toast(context);
    //            toast.setGravity(Gravity.CENTER, 0, 0); // Toast显示的位置
    //            toast.setDuration(Toast.LENGTH_SHORT); // Toast显示的时间
    //            toast.setView(view);
    //        }else{
    //            text = toast.getView().findViewById(R.id.textToast);
    //            text.setText(str); // 设置显示文字
    //        }
            toast.show();
        }
    }

    /**
     * 显示
     */
    public void ToastShow(Context context, ViewGroup root, String str) {
        if (context!=null){
            TextView text = null;
            ImageView img = null;
            if (toast==null){
                View view = LayoutInflater.from(context).inflate(R.layout.toast_layout, root);
                text = view.findViewById(R.id.textToast);
                text.setText(str); // 设置显示文字
                toast = new Toast(context);
                img = view.findViewById(R.id.imgToast);
                img.setImageResource(R.mipmap.icon_delete);
                toast.setGravity(Gravity.CENTER, 0, 0); // Toast显示的位置
                toast.setDuration(Toast.LENGTH_SHORT); // Toast显示的时间
                toast.setView(view);
            }else{
                text = toast.getView().findViewById(R.id.textToast);
                text.setText(str); // 设置显示文字
            }
            toast.show();
        }

    }


    @SuppressLint("WrongConstant")
    public void ToastLongShow(Context context, ViewGroup root, String str) {
        if (context!=null){
            TextView text = null;
            if (toast==null){
                View view = LayoutInflater.from(context).inflate(R.layout.toast_layout, root);
                text = view.findViewById(R.id.textToast);
                text.setText(str); // 设置显示文字
                toast = new Toast(context);
                toast.setDuration(10000); // Toast显示的时间
                toast.setGravity(Gravity.CENTER, 0, 0); // Toast显示的位置
                toast.setView(view);
            }else{
                text = toast.getView().findViewById(R.id.textToast);
                text.setText(str); // 设置显示文字
            }
            toast.show();
        }
    }

    public void ToastCancel() {
        if (toast != null) {
            toast.cancel();
        }
    }
}
